#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2022/7/12 14:36
@File: oceanbase.py
"""
import functools
import jaydebeapi
from .basic import DatabaseUtil

class OceanBaseUtil(DatabaseUtil):

    def __init__(self,user, password, ip, port=2883, **kwargs):
        self.CURSOR = {}
        if 'database' not in kwargs:
            if user.startswith('root'):
                database='oceanbase'
            else:
                database = user.split('@')[0]
        else:
            database=kwargs['database']
        jclassname = 'com.alipay.oceanbase.jdbc.Driver'
        url = f"jdbc:oceanbase://{ip}:{port}/{database}"
        driver_args = [user, password]
        jar_ile = self.getJarFile()
        self.db = jaydebeapi.connect(jclassname, url, driver_args, jar_ile)
        self.db.jconn.setAutoCommit(False)

    @property
    @functools.lru_cache()
    def type(self):
        try:
            sql="select version()"
            self.execsql(sql)
            return 'mysql'
        except Exception as e:
            return 'oracle'

if __name__ == '__main__':
    ip= '10.20.192.122'
    user = 'sys@oboracle#obtest'
    password = '123456'
    # user='root@sys'
    # password='root'
    a=OceanBaseUtil(user,password,ip)
    print(a.type)

